package com.ymatou.iapi.im.testcase;

import com.ymatou.iapi.im.parameter.DeleteSessionByUserIdBean;
import com.ymatou.iapi.im.parameter.GetMessageListAndSessionByTimelineBean;
import com.ymatou.iapi.im.parameter.SendMessageToBuyerBean;
import com.ymatou.iapi.im.service.DeleteSessionByUserIdCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymttest.business.service.ImCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.YmtMessage;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;

import java.util.List;
import java.util.Map;

/**
*用户删除会话
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_DeleteSessionByUserId{
	private static DeleteSessionByUserIdBean deletesessionbyuseridBean;
	private static DeleteSessionByUserIdCall deletesessionbyuseridCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户删除会话");
	}

	@Before
	public void caseUp() {
		deletesessionbyuseridBean=new DeleteSessionByUserIdBean();
		deletesessionbyuseridCall=new DeleteSessionByUserIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(deletesessionbyuseridCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DeleteSessionByUserId_01() {
		Logger.start(true,"用户删除会话,买手给买家发送信息");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			Integer fromUserId = 24635724;
			SendMessageToBuyerBean sendMessageToBuyerBean = ImCallService.initSendMessageToBuyerBean(fromUserId,userId,"nihao");
			String messageId = ImCallService.SendMessageToBuyer(sendMessageToBuyerBean).get("messageId").getAsString();

			GetMessageListAndSessionByTimelineBean getMessageListBean4 = ImCallService.initGetMessageListAndSessionByTimelineBean(fromUserId,userId);
			ImCallService.GetMessageListAndSessionByTimeline(getMessageListBean4);

			GetMessageListAndSessionByTimelineBean getMessageListBean5 = ImCallService.initGetMessageListAndSessionByTimelineBean(userId,fromUserId);
			ImCallService.GetMessageListAndSessionByTimeline(getMessageListBean5);

			//一方删除会话
			deletesessionbyuseridBean.setUserId(userId);
			deletesessionbyuseridBean.setOtherUserId(fromUserId);
			deletesessionbyuseridCall.setData(deletesessionbyuseridBean);
			deletesessionbyuseridCall.callService();
			Logger.verifyEquals("Success",deletesessionbyuseridCall.getString("Status"),"验证Status");
			ImDefaultWapper im = new ImDefaultWapper();
			YmtMessage ymtMessage = im.selectImInfo(messageId).get(0);
			Logger.verifyEquals(1,ymtMessage.getIsRead(),"验证isRead");
			Logger.verifyEquals(0,ymtMessage.getReceiverSee(),"验证receiverSee");
			Logger.verifyEquals(0,ymtMessage.getSenderSee(),"senderSee");
			if(fromUserId<userId){
				Logger.verifyEquals(1,ymtMessage.getBigSee(),"bigSee");
			}else{
				Logger.verifyEquals(1,ymtMessage.getSmallSee(),"smallSee");
			}
			Logger.verifyEquals(0,ymtMessage.getIsDelete(),"isDelete");

			List<Map> list2 = im.selectTotalCount(fromUserId,userId);
			Logger.verifyEquals(true,list2.isEmpty()?true:false,"验证session表为空");

			GetMessageListAndSessionByTimelineBean getMessageListBean = ImCallService.initGetMessageListAndSessionByTimelineBean(fromUserId,userId);
			ImCallService.GetMessageListAndSessionByTimeline(getMessageListBean);

			GetMessageListAndSessionByTimelineBean getMessageListBean1 = ImCallService.initGetMessageListAndSessionByTimelineBean(userId,fromUserId);
			ImCallService.GetMessageListAndSessionByTimeline(getMessageListBean1);

			//另一方也删除会话
            deletesessionbyuseridBean.setUserId(fromUserId);
            deletesessionbyuseridBean.setOtherUserId(userId);
            deletesessionbyuseridCall.setData(deletesessionbyuseridBean);
            deletesessionbyuseridCall.callService();
            Logger.verifyEquals("Success",deletesessionbyuseridCall.getString("Status"),"验证Status");

            YmtMessage ymtMessage1 = im.selectImInfo(messageId).get(0);
            Logger.verifyEquals(1,ymtMessage1.getIsRead(),"验证isRead");
            Logger.verifyEquals(1,ymtMessage1.getReceiverSee(),"验证receiverSee");
            Logger.verifyEquals(1,ymtMessage1.getSenderSee(),"senderSee");
            Logger.verifyEquals(1,ymtMessage1.getBigSee(),"bigSee");
            Logger.verifyEquals(1,ymtMessage1.getSmallSee(),"smallSee");
            Logger.verifyEquals(1,ymtMessage1.getIsDelete(),"isDelete");


			List<Map> list1 = im.selectTotalCount(userId,fromUserId);
			Logger.verifyEquals(true,list1.isEmpty()?true:false,"验证session表为空");

			GetMessageListAndSessionByTimelineBean getMessageListBean2 = ImCallService.initGetMessageListAndSessionByTimelineBean(fromUserId,userId);
			ImCallService.GetMessageListAndSessionByTimeline(getMessageListBean2);

			GetMessageListAndSessionByTimelineBean getMessageListBean3 = ImCallService.initGetMessageListAndSessionByTimelineBean(userId,fromUserId);
			ImCallService.GetMessageListAndSessionByTimeline(getMessageListBean3);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}